//
//  YZMStockSettlementView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/29.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockSettlementView.h"
#import "YZMStockSettlementCell.h"
#import "YZMResultView.h"
#import "YZMPageService.h"
#import "YZClient.h"
#import "YZMTradeSettlementPacket.h"
#import "YZMTradeSettlementModel.h"
#import <MJRefresh/MJRefresh.h>
#import "YZMStockSellNoneCell.h"
#import "YZMStockViewController.h"
#import "YZMOneyuanViewController.h"
#import "YZMSimulateViewController.h"
#import "YZClient+FreeExperience.h"
#import "YZMFreeExperienceViewController.h"

static NSString *const sYZMStockSettlementCellID = @"YZMStockSettlementCellID";
static NSString *const sYZMStockSellNoneCellID = @"YZMStockSellNoneCellID";

@interface YZMStockSettlementView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *refreshFooter;
@property (nonatomic, strong) YZMResultView *resultView;
@property (nonatomic, strong) YZMPageService *pageService;
@property (nonatomic, assign) BOOL unSuccessFetchFlag;
@property (nonatomic, assign) YZM_TRADE_TYPE tradeType;


@end

@implementation YZMStockSettlementView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style andTradeType:(YZM_TRADE_TYPE)tradeType
{
        self = [super initWithFrame:frame style:style];
        if (self) {
            _tradeType = tradeType;
            self.backgroundColor = [YZMAppStyle appBackgroundColor];
            _dataList = [NSMutableArray array];
            _pageService = [YZMPageService new];
            self.dataSource = self;
            self.delegate = self;
            self.separatorStyle = UITableViewCellSeparatorStyleNone;
            [self registerClass:[YZMStockSettlementCell class] forCellReuseIdentifier:sYZMStockSettlementCellID];
            [self registerClass:[YZMStockSellNoneCell class] forCellReuseIdentifier:sYZMStockSellNoneCellID];
    
            self.refreshHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(fetchSuggestionListFirstPage)];
            self.refreshHeader.stateLabel.textColor = [YZMAppStyle colorForLightGrayText];
            self.refreshHeader.stateLabel.font = [YZMAppStyle mediumSizeFont];
            self.refreshHeader.lastUpdatedTimeLabel.hidden = YES;
    
            self.mj_header = _refreshHeader;
    
            self.refreshFooter = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self fetchSuggestionListNextPage];
            }];
    
            self.refreshFooter.stateLabel.textColor = [YZMAppStyle colorForLightGrayText];
            self.refreshFooter.stateLabel.font = [YZMAppStyle mediumSizeFont];
    
            self.mj_footer = _refreshFooter;
    
            self.resultView = [YZMResultView new];
            self.resultView.backgroundColor = [YZMAppStyle appBackgroundColor];
            [self addSubview:self.resultView];
            [self.resultView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.width.height.equalTo(self);
            }];
    
            __weak __typeof(&*self) weakSelf = self;
            self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
            self.resultView.retryAction = ^(){
                switch (weakSelf.tradeType) {
                    case YZM_TRADE_TYPE_FOR_A_STOCK:
                        [weakSelf fetchItemList];
                        break;
                    case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
                        [weakSelf fetchItemYlbList];
                        break;
                    case YZM_TRADE_TYPE_FOR_A_STOCK_D:
                        [weakSelf fetchItemListForD];
                        break;

                    case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
                       [weakSelf fetchItemListForOneY];
                        break;
                    case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:{
                        [weakSelf fetchItemListForFreeExperience];
                    }
                        break;

                    case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:
                        [weakSelf fetchItemListForSimU];
                        break;
                    default:
                        break;
                }
            };
    
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
                                                         name:AFNetworkingReachabilityDidChangeNotification object:nil];
        }
        return self;
}

- (void)setModel:(YZMMyStackModel *)model {
    
    _model = model;
    [self fetchSuggestionListFirstPage];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)reachabilityChanged:(NSNotification *)note{
    
    if (!kNetworkNotReachability) {
        
        [self fetchSuggestionListFirstPage];
    }
}

#pragma mark -- network

- (void)fetchSuggestionListFirstPage{
    
    [_pageService preChangeWithPage:_pageService.minPage andTimeEnd:YES];
    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
            [self fetchItemList];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
            [self fetchItemYlbList];
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_D:
                if (self.model) {
            
                    [self fetchItemListForD];
                } else {
        
                    [self.refreshFooter resetNoMoreData];
                    self.refreshFooter.stateLabel.text = @"";
                    [self endRefresh];
                }
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
                [self fetchItemListForOneY];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:{
            [self fetchItemListForFreeExperience];
        }
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:
                [self fetchItemListForSimU];
            break;
            
        default:
            break;
    }
}

- (void)fetchSuggestionListNextPage{
    
    [_pageService preChangeWithPage:_pageService.nextPage  andTimeEnd:YES];
    
    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
            [self fetchItemList];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
            [self fetchItemYlbList];
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_D:
            [self fetchItemListForD];
            break;
            
        case  YZM_TRADE_TYPE_FOR_A_STOCK_ONE:
            [self fetchItemListForOneY];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:{
            [self fetchItemListForFreeExperience];
        }
            break;

        case  YZM_TRADE_TYPE_FOR_A_STOCK_SIM:
            [self fetchItemListForSimU];
            break;
        
        default:
            break;
    }
}

- (void)endRefresh{
    
    if (_refreshHeader.isRefreshing) {
        
        [_refreshHeader endRefreshing];
    }
    
    if (_refreshFooter.isRefreshing) {
        
        [_refreshFooter endRefreshing];
    }
}

- (void)fetchItemList{
    
    /*无网处理*/
    if (kNetworkNotReachability) {
        
        [self endRefresh];
        
        if ( self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
            
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        
        if (self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient td_fetchTradeSettlementListWithPage:self.pageService.tempPage andPageSize:self.pageService.pageSize andTimeEnd:_pageService.tempTimeEnd success:^(NSURLResponse *response, id responseObject) {
        
        [self endRefresh];
        
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            
            YZMTradeSettlementPacket *result = topModel.data;
            self.unSuccessFetchFlag = YES;
            
            NSArray *sList = result.items;
            NSInteger pageIndex = result.currentPage;
            
            if (pageIndex == _pageService.minPage) {
                
                [self.dataList removeAllObjects];
                [self.dataList addObjectsFromArray:sList];
//                _refreshFooter.hidden=YES;
            }else{
                [self.dataList addObjectsFromArray:sList];
            }
            
            if (result.currentPage >= result.totalPage) {
                
                if (self.dataList.count == 0) {
                    self.mj_footer = nil;
                }else{
                    _refreshFooter.state = MJRefreshStateNoMoreData;
                }
            }else{
                _refreshFooter.state = MJRefreshStateIdle;
            }
            
            [self reloadData];
            [_pageService effectChange];
        }
        else{
            if (self.dataList.count == 0) {
                [_resultView syncStatusWithError:[NSError yz_errorWithMessage:topModel.resultMessage]];
                self.mj_header = nil;
                self.mj_footer = nil;
            }else{
                [topModel showMessage];
            }
        }
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [error yz_showMessage];
        }
    }];
}


- (void)fetchItemYlbList{
    
    /*无网处理*/
    if (kNetworkNotReachability) {
        
        [self endRefresh];
        
        if ( self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
            
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        
        if (self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient td_fetchTradeYlbSettlementListWithPage:self.pageService.tempPage andPageSize:self.pageService.pageSize andTimeEnd:_pageService.tempTimeEnd success:^(NSURLResponse *response, id responseObject) {
        
        [self endRefresh];
        
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        
        if (topModel.success) {
            
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            
            YZMTradeSettlementPacket *result = topModel.data;
            self.unSuccessFetchFlag = YES;
            
            NSArray *sList = result.items;
            NSInteger pageIndex = result.currentPage;
            
            if (pageIndex == _pageService.minPage) {
                
                [self.dataList removeAllObjects];
                [self.dataList addObjectsFromArray:sList];
                //                _refreshFooter.hidden=YES;
            }else{
                [self.dataList addObjectsFromArray:sList];
            }
            
            if (result.currentPage >= result.totalPage) {
                
                if (self.dataList.count == 0) {
                    self.mj_footer = nil;
                }else{
                    _refreshFooter.state = MJRefreshStateNoMoreData;
                }
            }else{
                _refreshFooter.state = MJRefreshStateIdle;
            }
            
            [self reloadData];
            [_pageService effectChange];
        }
        else{
            if (self.dataList.count == 0) {
                [_resultView syncStatusWithError:[NSError yz_errorWithMessage:topModel.resultMessage]];
                self.mj_header = nil;
                self.mj_footer = nil;
            }else{
                [topModel showMessage];
            }
        }
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [error yz_showMessage];
        }
    }];
}

-(void)fetchItemListForD
{
    /*无网处理*/
    if (kNetworkNotReachability) {
        
        [self endRefresh];
        
        if ( self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
            
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        
        if (self.dataList.count == 0) {
            
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient td_fetchTradeSettlementListForDWithPage:self.pageService.tempPage andPageSize:self.pageService.pageSize withID:self.model.Id success:^(NSURLResponse *response, id responseObject) {
        [self endRefresh];
        
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        [self endRefresh];
        
        YZGeneralResultModel *topModel = responseObject;

        if (topModel.success) {
            [[NSNotificationCenter defaultCenter] removeObserver:self];
            
            YZMTradeSettlementPacket *result = topModel.data;
            self.unSuccessFetchFlag = YES;
            
            NSArray *sList = result.items;
            NSInteger pageIndex = result.currentPage;
            
            if (pageIndex == _pageService.minPage) {
                
                [self.dataList removeAllObjects];
                [self.dataList addObjectsFromArray:sList];
//                _refreshFooter.hidden=YES;
            }else{
                
                [self.dataList addObjectsFromArray:sList];
            }
            
            if (result.currentPage >= result.totalPage) {
                
                if (self.dataList.count == 0) {
                    
                    self.mj_footer = nil;
                }else{
                    _refreshFooter.state = MJRefreshStateNoMoreData;
                }
                
            }else{
                
                _refreshFooter.state = MJRefreshStateIdle;
            }
            [self reloadData];
            [_pageService effectChange];
        }
        else
        {
            if (self.dataList.count == 0) {
                
                [_resultView syncStatusWithError:[NSError yz_errorWithMessage:topModel.resultMessage]];
                self.mj_header = nil;
                self.mj_footer = nil;
                
            }else{
                
                [topModel showMessage];
            }
        }
        
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [self endRefresh];
            [error yz_showMessage];
        }
    }];
}

- (void)fetchItemListForOneY{
    
    _refreshFooter.hidden=YES;
    /*无网处理*/
    if (kNetworkNotReachability) {
        [self endRefresh];
        if ( self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        if (self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient oneY_getSettleListWithParamsuccess:^(NSURLSessionDataTask *task, id responseObject) {
        [self endRefresh];
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            [[NSNotificationCenter defaultCenter]removeObserver:self];
            YZMTradeSettlementModel *model=topModel.data;
            self.unSuccessFetchFlag = YES;
            NSArray *listArray=(NSArray *)model;
             [self.dataList removeAllObjects];
            [self.dataList addObjectsFromArray:listArray];
        }else{
            [topModel showMessage];
        }
        [self reloadData];
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [self endRefresh];
            [error yz_showMessage];
        }
    }];
}

- (void)fetchItemListForFreeExperience{
    
    _refreshFooter.hidden=YES;
    /*无网处理*/
    if (kNetworkNotReachability) {
        [self endRefresh];
        if ( self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        if (self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient freeEx_getSettleListWithParamsuccess:^(NSURLSessionDataTask *task, id responseObject) {
        [self endRefresh];
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            [[NSNotificationCenter defaultCenter]removeObserver:self];
            YZMTradeSettlementModel *model=topModel.data;
            self.unSuccessFetchFlag = YES;
            NSArray *listArray=(NSArray *)model;
            [self.dataList removeAllObjects];
            [self.dataList addObjectsFromArray:listArray];
        }else{
            [topModel showMessage];
        }
        [self reloadData];
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [self endRefresh];
            [error yz_showMessage];
        }
    }];
}


- (void)fetchItemListForSimU{
    _refreshFooter.hidden=YES;
    /*无网处理*/
    if (kNetworkNotReachability) {
        [self endRefresh];
        if ( self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_NO_WIFI;
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (!YZMUserServiceInstance.isValidate) {
        if (self.dataList.count == 0) {
            self.resultView.status = YZM_RESULT_STATUS_FOR_ABNORMAL;
        }else{
            [NSError yz_showMessageForNoWifi];
        }
        return;
    }
    
    if (self.dataList.count == 0) {
        _resultView.status = YZM_RESULT_STATUS_FOR_LOADING;
    }
    
    [YZClient simU_getSettleListDataSuccess:^(NSURLResponse *response, id responseObject) {
        [self endRefresh];
        self.mj_header = _refreshHeader;
        self.mj_footer = _refreshFooter;
        self.resultView.status = YZM_RESULT_STATUS_FOR_NORMAL;
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            [[NSNotificationCenter defaultCenter]removeObserver:self];
            YZMTradeSettlementModel *model=topModel.data;
            self.unSuccessFetchFlag = YES;
            NSArray *listArray=(NSArray *)model;
            [self.dataList removeAllObjects];
            [self.dataList addObjectsFromArray:listArray];
        }else{
            [topModel showMessage];
        }
        [self reloadData];
    } failure:^(NSError *error) {
        [self endRefresh];
        //网络异常处理
        if (self.dataList.count == 0) {
            [_resultView syncStatusWithError:error];
            self.mj_header = nil;
            self.mj_footer = nil;
        }else{
            [self endRefresh];
            [error yz_showMessage];
        }
    }];
}


#pragma mark -- action

- (void)buyStockPressed {
    YZMStockViewController *tradeVC = nil;
    switch (self.tradeType) {
        case YZM_TRADE_TYPE_FOR_A_STOCK:
            tradeVC = [YZMHCMInstance instanceWithControllerName:@"stock"];
            [tradeVC showEntrustListPage];
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_YLB:
            tradeVC = [YZMHCMInstance instanceWithControllerName:@"stockYLB"];
            [tradeVC showEntrustListPage];
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_D:
            tradeVC = [YZMHCMInstance instanceWithControllerName:@"stockTD"];
            [tradeVC showEntrustListPage];
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_ONE:{
            YZMOneyuanViewController *oneVC = [YZMHCMInstance instanceWithControllerName:@"oneYuan"];
            [oneVC showEntrustListPage];}
            break;
            
        case YZM_TRADE_TYPE_FOR_A_STOCK_SIM:{
            YZMSimulateViewController *simVC = [YZMHCMInstance instanceWithControllerName:@"Simulate"];
            [simVC showEntrustListPage];}
            break;
        case YZM_TRADE_TYPE_FOR_A_STOCK_FREE:{
            YZMFreeExperienceViewController *oneVC = [YZMHCMInstance instanceWithControllerName:@"freeExperience"];
            [oneVC showEntrustListPage];
        }
            break;
            
        default:
            break;
    }
}

#pragma mark TableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataList.count == 0) {
        YZMStockSellNoneCell *cell = [tableView dequeueReusableCellWithIdentifier:sYZMStockSellNoneCellID forIndexPath:indexPath];
        __weak __typeof(&*self) weakSelf = self;
        cell.addAction = ^(){
            [weakSelf buyStockPressed];
        };
        return cell;
    }
    
    YZMStockSettlementCell *cell = [tableView dequeueReusableCellWithIdentifier:sYZMStockSettlementCellID forIndexPath:indexPath];
    cell.tradeType = _tradeType;
    cell.record = self.dataList[indexPath.row];
    YZMIndexCount indexCount;
    indexCount.index = indexPath.row;
    indexCount.count = self.dataList.count;
    cell.indexCount = indexCount;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count?self.dataList.count:1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.dataList.count == 0) {
        return self.frame.size.height - self.tableHeaderView.frame.size.height - 50;
    }
    if (_tradeType == YZM_TRADE_TYPE_FOR_A_STOCK_FREE) {
        YZMTradeSettlementModel *record = self.dataList[indexPath.row];
        if ([record.userProfit integerValue] > 0) {
            return 70 + [self heightForFreeInfoLabel];
        }
    }
    return 70.0;
}

- (CGFloat)heightForFreeInfoLabel{
    CGFloat height = [@"本次免费体验活动所获得的红包金额%@元已发放至您的红包账户，可用于抵扣交易综合费。（注：盈利金额四舍五入取整后即为您的红包金额）" boundingRectWithSize:CGSizeMake(ScreenWidth - 60, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0]} context:nil].size.height;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return  [UIView new];
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row >= _dataList.count) {
        
        return;
    }
    YZMTradeSettlementModel *instance = _dataList[indexPath.row];

    [YZMCMInstance pushViewControllerWithName:@"stockDetail" andParams:@{@"trade": instance,@"tradetype":@(self.tradeType)}];
}

@end
